package com.ophiux.gateway.exception;

import com.alibaba.csp.sentinel.Tracer;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import com.alibaba.nacos.api.model.v2.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @desc:
 * @author: hhl
 * @date：2023/5/24 15:04
 */
@RestController
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result handleGlobalException(Exception e) {
        log.error("全局异常信息 ex={}", e.getMessage(), e);
        // 业务异常交由 sentinel 记录
        Tracer.trace(e);
        return Result.failure(e.getLocalizedMessage());
    }

    @ExceptionHandler(ParamFlowException.class)
    public Result handleException(ParamFlowException e) {
        log.info("sentinel流控处理");
        return Result.failure("sentinel流控处理");
    }
}
